package br.ucb.sigplan.dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.ucb.sigplan.beans.Calendario;
import br.ucb.sigplan.connection.FactoryCon;
import br.ucb.sigplan.dao.CalendarioDAO;
import br.ucb.sigplan.exception.DAOException;
import br.ucb.sigplan.util.DataUtil;

public class CalendarioDAOImpl implements CalendarioDAO {
	private final String SQL_DELETE = "delete from calendario where idcalendario = ?";
	private final String SQL_UPDATE = "update calendario set data = ?, descricao = ?, tipo = ? where idcalendario = ?";
	private final String SQL_FINDALL = "select * from calendario";
	private final String SQL_INSERT = "insert into calendario (data, descricao, tipo) values (?,?,?)";
	private final String SQL_SEARCHBYID = "select * from calendario where idcalendario = ?";
	private final String SQL_SEACHBYTIPO = "select * from calendario where tipo = ?";
	private final String SQL_SEARCHBYDATA = "select * from calendario where data = ? and descricao = ?";

	public void delete(Integer id) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		try {
			instrucao = con.prepareStatement(SQL_DELETE);
			instrucao.setInt(1, id);
			instrucao.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("feriadoDelecao");
		} finally {
			FactoryCon.closeConnection(con, instrucao);
		}
	}

	public List<Calendario> findAll() throws DAOException {
		List<Calendario> dias = new ArrayList<Calendario>();
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		ResultSet set = null;
		Calendario calendario;
		try {
			instrucao = con.prepareStatement(SQL_FINDALL);
			set = instrucao.executeQuery();
			while (set.next()) {
				calendario = new Calendario();
				putCalendario(set, calendario);
				dias.add(calendario);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroPesquisa");
		} finally {
			FactoryCon.closeConnection(con, instrucao, set);
		}
		return dias;
	}
	
	private void putCalendario(ResultSet set, Calendario calendario) throws SQLException {
		calendario.setIdCalendario(set.getInt("idcalendario"));
		calendario.setData(DataUtil.getDataFromBanco(set.getDate("data")));
		calendario.setDescricao(set.getString("descricao"));
		calendario.setTipo(set.getString("tipo").charAt(0));
	}

	public Calendario findByChave(Integer id) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		ResultSet set = null;
		Calendario calendario = null;
		try {
			instrucao = con.prepareStatement(SQL_SEARCHBYID);
			instrucao.setInt(1, id);
			set = instrucao.executeQuery();
			while (set.next()) {
				calendario = new Calendario();
				putCalendario(set, calendario);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroPesquisa");
		} finally {
			FactoryCon.closeConnection(con, instrucao, set);
		}
		return calendario;
	}

	public void insert(Calendario calendario) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		try {
			instrucao = con.prepareStatement(SQL_INSERT);
			instrucao.setDate(1, new java.sql.Date(calendario.getData().getTime().getTime()));
			instrucao.setString(2, calendario.getDescricao());
			instrucao.setString(3, calendario.getTipo().toString());
			instrucao.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroInsercao");
		} finally {
			FactoryCon.closeConnection(con, instrucao);
		}
	}

	public void update(Calendario calendario) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		try {
			instrucao = con.prepareStatement(SQL_UPDATE);
			instrucao.setDate(1, new Date(calendario.getData().getTime().getTime())) ;
			instrucao.setString(2, calendario.getDescricao());
			instrucao.setString(3, calendario.getTipo().toString());
			instrucao.setInt(4, calendario.getIdCalendario());
			instrucao.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroAtualizacao");
		} finally {
			FactoryCon.closeConnection(con, instrucao);
		}
	}

	@Override
	public List<Calendario> findDatas(Character tipo) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		ResultSet set = null;
		Calendario calendario;
		List<Calendario> datas = new ArrayList<Calendario>();
		try {
			instrucao = con.prepareStatement(SQL_SEACHBYTIPO);
			instrucao.setString(1, tipo.toString());
			set = instrucao.executeQuery();
			while (set.next()) {
				calendario = new Calendario();
				putCalendario(set, calendario);
				datas.add(calendario);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroPesquisa");
		} finally {
			FactoryCon.closeConnection(con, instrucao, set);
		}
		return datas;
	}
	
	@Override
	public Calendario findByData(java.util.Date data, String descricao) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		ResultSet set = null;
		Calendario calendario = null;
		try {
			instrucao = con.prepareStatement(SQL_SEARCHBYDATA);
			instrucao.setDate(1, new java.sql.Date(data.getTime()));
			instrucao.setString(2, descricao);
			set = instrucao.executeQuery();
			if (set.next()) {
				calendario = new Calendario();
				putCalendario(set, calendario);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroPesquisa");
		} finally {
			FactoryCon.closeConnection(con, instrucao, set);
		}
		return calendario;
	}

}
